// pages/shipment/upload/upload.js
import {
  actualShipment,
  abortShipment,
  completeShipment,
  uploadImage
} from '../../../utils/api'
import {nowDate, nowDateTime} from '../../../util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: null,//actualShipment 实际发货 abortShipment 终止配送
    shipmentId: null,
    id: null,
    imageList: [],
    reason: null
  },
  navigateBackAndRefresh(){
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setData({
      id: this.data.id // 设置上个页面的配送单id， 刷新用
    })
    wx.navigateBack({
      delta: 1
    })
  },
  //点击确认
  async confirmUpload() {
    let{type, shipmentId, imageList, reason} = this.data
    let dataList = imageList.map(i => wx.getFileSystemManager().readFileSync(i.tempFilePath, "base64"))
    let fileNames = []
    for (const img of dataList) {
      await uploadImage({
        imgData: img
      }, res=> {
        fileNames.push(res.result.fileName)
      });
    }
    console.log('upload.js - fileNames: ' + fileNames.join(','))
    let now = nowDateTime();
    switch(type){
      case 'actualShipment':
        actualShipment({
          fileNames: fileNames.join(','),
          shipDate: now,
          shipmentId:shipmentId,
          remark: reason
        }, res => {
          this.navigateBackAndRefresh()
        })
        break;
      case 'abortShipment':
        abortShipment({
          abortDate: now,
          abortReason: reason,
          fileNames: fileNames.join(','),
          shipmentId: shipmentId
        }, res => {
          wx.switchTab({
            url: '/pages/shipment/list/list',
          })
        })
        break;
      case 'completeShipment':
        completeShipment({
          completeDate: now,
          remark: reason,
          fileNames: fileNames.join(','),
          shipmentId: shipmentId
        }, res => {
          this.navigateBackAndRefresh()
        })
    }
  },
  bindReason(e) {
    this.setData({
      reason: e.detail
    })
  },
  chooseImage() {
    let that = this
    let imageList = that.data.imageList
    wx.chooseMedia({
      mediaType: ['image'],//只允许图片
      success: (res) => {
        imageList.push(...res.tempFiles)
        that.setData({
          imageList: imageList
        })
      }
    })
  },
  //预览图片
  previewImg(e) {
    let index = e.currentTarget.dataset.index;
    let img_arr = this.data.imageList.map(i => i.tempFilePath);
    wx.previewImage({
      current: img_arr[index].tempFilePath,
      urls: img_arr
    })
  },
  //删除照片功能与预览照片功能 
  deleteImg(e) {
    let img_arr = this.data.imageList;
    let index = e.currentTarget.dataset.index;  //获取长按删除图片的index
    wx.showModal({
      content: '确定要删除此图片吗？',
      success: res => {
        if (res.confirm) {
          img_arr.splice(index, 1);
        } else if (res.cancel) {
          return false;
        }
        this.setData({
          imageList: img_arr
        });
      }
    })
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    getApp().checkLogin()
    this.setData({
      shipmentId: options.shipmentId,
      type: options.type,
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    getApp().checkLogin()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})